package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.SysMenu;
import com.itheima.youdianda.mapper.SysMenuMapper;
import com.itheima.youdianda.service.SysMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-02-12 14:26
 * @Desc 菜单Service实现类
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    /**
     * 菜单删除
     * @Author: Pan Rundong
     * @Date: 2023-02-12 16:32
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param id
     * @return void
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteMenu(int id) {
        //1.创建一个id集合，收集所有要删除的id
        List<Integer> idList = new ArrayList<>();
        //2.递归，找到所有要删除的id
        this.getIds(id, idList);
        //3.存入当前id
        idList.add(id);
        //4.统一删除
        this.removeByIds(idList);
    }

    /**
     * 根据Id获取菜单详情
     * @Author: Pan Rundong
     * @Date: 2023-02-13 13:50
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param id
     * @return com.itheima.youdianda.domain.SysMenu
     */
    @Override
    public SysMenu getMenu(Integer id) {
        //1.设置条件过滤器
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getId, id);
        SysMenu sysMenu = this.getOne(queryWrapper);
        return sysMenu;
    }

    /**
     * 返回所有菜单
     * @Author: Pan Rundong
     * @Date: 2023-02-12 17:50
     * @Greet2u:  （O｀ 3′O）~~♡
     * @return java.util.List<com.itheima.youdianda.domain.SysMenu>
     */
    @Override
    public List<SysMenu> getAllData() {
        //1.设置条件，找到所有父id为0的对象
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getPid, 0);
        queryWrapper.eq(SysMenu::getDelFlag,0);
        List<SysMenu> sysMenuList = this.list(queryWrapper);

        //3.获取子菜单
        sysMenuList.stream().forEach((item)->{
            item.setChildren(new ArrayList<>());
            item.getChildren().add(selectChildren(item));
        });
        return sysMenuList;
    }

    /**
     * 递归找子表
     * @Author: Pan Rundong
     * @Date: 2023-02-13 9:53
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param sysMenu
     * @return void
     */
    private SysMenu selectChildren(SysMenu sysMenu) {
        //1.初始化子表
        sysMenu.setChildren(new ArrayList<>());
        //2.找孙表
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getPid, sysMenu.getId());
        queryWrapper.eq(SysMenu::getDelFlag,0);
        List<SysMenu> list = this.list(queryWrapper);

        //3.对孙表遍历
        list.stream().forEach((item) -> {
            if (sysMenu.getChildren() == null) {
                sysMenu.setChildren(new ArrayList<>());
            }
            sysMenu.getChildren().add(selectChildren(item));
        });
        return sysMenu;
    }

    /**
     * 递归算法
     * @Author: Pan Rundong
     * @Date: 2023-02-12 17:25
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param id
     * @param idList
     * @return void
     */
    private void getIds(int id, List<Integer> idList) {
        //1.创建条件过滤器
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getPid, id);
        queryWrapper.isNull(SysMenu::getDefaultData);
        queryWrapper.select(SysMenu::getId);
        //2.查找子表
        List<SysMenu> childrenList = this.list(queryWrapper);
        childrenList.stream().forEach((item)->{
            //2.1将当前菜单id存入list
            idList.add(item.getId());
            //2.2递归查询
            this.getIds(item.getId(),idList);
        });
    }
}
